package com.tutor.lzm.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.tutor.lzm.R;
import com.tutor.lzm.bean.TeacherRecommendBean;
import com.tutor.lzm.utils.ProjectStringUtils;
import com.tutor.lzm.view.RoundImageView;
import com.tutor.syslib.Logs;
import com.tutor.syslib.util.TextUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by zzgpc on 2017-9-6.
 * 老师列表适配器（热门老师推荐，）
 */
public class TeacherAdapter extends BaseAdapter {
    private Context mCtx;
    private List<TeacherRecommendBean> mShowList;

//    public TeacherAdapter(Context context) {
//        this.mCtx = context;
//
//    }

    public TeacherAdapter(Context context, List<TeacherRecommendBean> list) {
        this.mCtx = context;
        this.mShowList = list;

    }

    @Override
    public int getCount() {
        return mShowList.size();
    }

    @Override
    public Object getItem(int position) {
        return mShowList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Logs.d("TeacherAdapter", "当前位置 position = " + position);
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mCtx).inflate(R.layout.item_teacher, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        TeacherRecommendBean bean = mShowList.get(position);
        Picasso.with(mCtx)
                .load(bean.headImage)
                .error(R.mipmap.personal_default)
                .into(viewHolder.imgTeacher);
        if (!TextUtil.isNull(bean.name)) {
            viewHolder.tvTitle.setText(bean.name + "老师");
        }
        String status = ProjectStringUtils.getTeacherStatus(bean.status);
        viewHolder.tvStatus.setText(status);
        viewHolder.ratingBar.setRating(5);
        if (bean.isHot == 1) {
            viewHolder.tvHotStatus.setVisibility(View.VISIBLE);
        } else {
            viewHolder.tvHotStatus.setVisibility(View.VISIBLE);
        }
        String categorytypeText;
        if (TextUtil.isNull(bean.categorytype)) {
            categorytypeText = ProjectStringUtils.getGradeForID(bean.categoryId);
        } else {
            categorytypeText = bean.categorytype;
        }
        viewHolder.tvTeachLevel.setText(categorytypeText + "  " + bean.categoryIdText);
        viewHolder.tvTeacherPrice.setText(bean.price + "元/小时");
        Drawable drawable = mCtx.getResources().getDrawable(R.mipmap.gouxuan);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), (int) (drawable.getMinimumHeight()));
        if (bean.isAuthentication == 1) {
            viewHolder.tvRealName.setCompoundDrawables(drawable, null, null, null);
        }

        if (bean.isDegree == 1) {
            viewHolder.tvEducation.setCompoundDrawables(drawable, null, null, null);
        }
        if (bean.isTeacherCertification == 1) {
            viewHolder.tvQualification.setCompoundDrawables(drawable, null, null, null);
        }
        return convertView;
    }


    class ViewHolder {
        @BindView(R.id.img_teacher)
        RoundImageView imgTeacher;
        @BindView(R.id.tv_title)
        TextView tvTitle;
        @BindView(R.id.tv_status)
        TextView tvStatus;
        @BindView(R.id.tv_hot_status)
        TextView tvHotStatus;
        @BindView(R.id.tv_benefit_status)
        TextView tvBenefitStatus;
        @BindView(R.id.tv_teach_level)
        TextView tvTeachLevel;
        @BindView(R.id.ratingBar)
        RatingBar ratingBar;
        @BindView(R.id.tv_real_name)
        TextView tvRealName;
        @BindView(R.id.tv_qualification)
        TextView tvQualification;
        @BindView(R.id.tv_education)
        TextView tvEducation;
        @BindView(R.id.tv_teach_price)
        TextView tvTeacherPrice;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
